package com.cf.crudweb.controller;


import com.cf.web.RequiredLogin;
import com.cf.constant.EnumReturnCode;
import com.cf.crud.entity.BaseEntity;
import com.cf.crud.service.BaseService;
import com.cf.exception.BizException;
import com.cf.id.SnowflakeId;
import com.cf.vo.ResponseVo;
import com.cf.web.controller.PageController;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Map;

/**
 * cf-game-CommomController
 *
 * @author <a href="2428922347@qq.com">Zhu mingming</a>
 * @info
 * @since 2018/3/1 18:07
 */
public abstract class CommonController<Entity extends BaseEntity, Service extends BaseService<Entity>> extends PageController {

    @Autowired
    private Service service;

    @Autowired
    private SnowflakeId idService;

    //################################################################################
    //---------------------------------------增---------------------------------------
    //################################################################################

    /**
     * 创建
     *
     * @return
     */
    @ResponseBody
    @RequiredLogin
    @ApiOperation(value = "创建", notes = "根据post传来的值创建对象")
    @RequestMapping(method = {RequestMethod.POST})
    public ResponseVo<Entity> create(@Valid @RequestBody Entity entity) throws BizException {
        long nextId = idService.nextId();
        entity.setId(nextId);
        Entity insert = service.insert(entity);
        return insert != null ? ResponseVo.success(insert, "创建成功！") : ResponseVo.fail(EnumReturnCode.ERROR.getCode(), insert, "创建失败！");
    }


    //################################################################################
    //---------------------------------------删---------------------------------------
    //################################################################################

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @ResponseBody
    @RequiredLogin
    @ApiOperation(value = "删除", notes = "根据Id删除数据")
    @RequestMapping(value = "/{id}", method = {RequestMethod.DELETE})
    @ApiImplicitParam(name = "id", value = "数据id", required = true, dataType = "string", paramType = "path")
    public ResponseVo<Integer> delete(@PathVariable("id") long id) {
        Integer deleteNum = service.deleteById(id);
        return deleteNum > 0 ? ResponseVo.success(deleteNum, "删除成功！") : ResponseVo.fail(EnumReturnCode.ERROR.getCode(), deleteNum, "删除数据不存在！");
    }

    //################################################################################
    //---------------------------------------改---------------------------------------
    //################################################################################

    /**
     * 更新
     *
     * @return
     */
    @ResponseBody
    @RequiredLogin
    @ApiOperation(value = "更新", notes = "根据Id更新其他数据，为空的不会被更新")
    @RequestMapping(method = {RequestMethod.PUT})
    public ResponseVo<PageInfo<Entity>> update(@Valid @RequestBody Entity entity) {
        long id = entity.getId();
        if (id == 0)
            return ResponseVo.fail(EnumReturnCode.ERROR.getCode(), null, "更新ID不能为空！");
        int update = service.update(entity);
        return update > 0 ? ResponseVo.success(update, "更新成功！") : ResponseVo.fail(EnumReturnCode.ERROR.getCode(), null, "更新失败！");
    }


    //################################################################################
    //---------------------------------------查---------------------------------------
    //################################################################################

    /**
     * 更具ID获取
     *
     * @param id
     * @return
     */
    @ResponseBody
    @RequiredLogin
    @ApiOperation(value = "获取数据", notes = "根据Id获取数据")
    @RequestMapping(value = "/{id}", method = {RequestMethod.GET})
    @ApiImplicitParam(name = "id", value = "数据ID", required = true, dataType = "string", paramType = "path")
    public ResponseVo<Integer> get(@PathVariable("id") long id) {
        Entity entity = service.selectOne(id);
        return entity != null ? ResponseVo.success(entity, "检索成功！") : ResponseVo.fail(EnumReturnCode.ERROR.getCode(), entity, "未检索到数据！");
    }

    /**
     * 条件检索
     *
     * @param params
     * @param page
     * @param size
     * @return
     */
    @ResponseBody
    @RequiredLogin
    @ApiOperation(value = "检索数据", notes = "根据提交检索数据")
    @RequestMapping(value = "/search", method = {RequestMethod.POST})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页数", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "单页数据量", required = true, dataType = "string", paramType = "query")
    })
    public ResponseVo<PageInfo<Entity>> search(
            @RequestBody Map<String, Object> params,
            @RequestParam(value = "page", defaultValue = "0") Integer page,
            @RequestParam(value = "size", defaultValue = "20") Integer size) {

        Page<Entity> entities = service.commonSearch(params, page, size);
        return ResponseVo.success(new PageInfo(entities), "检索成功！");
    }

    /**
     * 条件检索
     *
     * @param params
     * @param page
     * @param size
     * @return
     */
    @ResponseBody
    @RequiredLogin
    @ApiOperation(value = "检索数据", notes = "根据提交检索数据")
    @RequestMapping(value = "/search", method = {RequestMethod.GET})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页数", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "单页数据量", required = true, dataType = "string", paramType = "query")
    })
    public ResponseVo<PageInfo<Entity>> searchByGet(
            @RequestParam Map<String, Object> params,
            @RequestParam(value = "page", defaultValue = "0") Integer page,
            @RequestParam(value = "size", defaultValue = "20") Integer size) {
        Page<Entity> entities = service.commonSearch(params, page, size);
        return ResponseVo.success(new PageInfo(entities), "检索成功！");
    }

    /**
     * 分页获取
     *
     * @param page
     * @param size
     * @returnthiz.back();
     */
    @ResponseBody
    @RequiredLogin
    @ApiOperation(value = "分页获取数据", notes = "根据页码检索数据")
    @RequestMapping(method = {RequestMethod.GET})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页数", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "单页数据量", required = true, dataType = "string", paramType = "query")
    })
    public ResponseVo<PageInfo<Entity>> getPage(
            @RequestParam(value = "page", defaultValue = "0") Integer page,
            @RequestParam(value = "size", defaultValue = "20") Integer size) {


        Page<Entity> entities = service.selectByPage(page, size);
        return ResponseVo.success(new PageInfo(entities), "检索成功！");
    }


    protected Service getService() {
        return service;
    }
}
